package com.mianyang.zhangsan.service.impl;

import com.mianyang.zhangsan.common.constant.SecConstant;
import com.mianyang.zhangsan.common.util.FileUtils;
import com.mianyang.zhangsan.common.util.OperationManHelper;
import com.mianyang.zhangsan.model.dto.UploadData;
import com.mianyang.zhangsan.model.entity.SecFile;
import com.mianyang.zhangsan.mapper.SecFileMapper;
import com.mianyang.zhangsan.model.entity.SecVideo;
import com.mianyang.zhangsan.plugin.oss.MinioService;
import com.mianyang.zhangsan.service.ISecFileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.io.OutputStream;
import java.time.LocalDate;
import java.util.Date;

/**
 *
 * @author jiangjr
 * @since 2022-10-23
 */
@Service
public class SecFileServiceImpl extends ServiceImpl<SecFileMapper, SecFile> implements ISecFileService {
    @Autowired
    private MinioService minioService;

    @Override
    public UploadData uploadFile(MultipartFile file, int fileType) {
        // 上传文件至文件服务器
        String filePath = minioService.uploadFile(file, SecConstant.DEFAULT_BUCKET);
        // 保存文件信息
        String fileName = file.getOriginalFilename();
        Long accountId = OperationManHelper.getAccountId();
        String accountName = OperationManHelper.getAccountName();
        // 保存视频信息

        // 将文件信息保存到数据库
        SecFile secFile = SecFile.builder()
                .fileName(fileName)
                .filePath(filePath)
                .menuId(1L)
                .fileSize(file.getSize())
                .uploadId(accountId)
                .uploadName(accountName)
                .uploadTime(new Date())
                .fileType(fileType).build();
        save(secFile);
        UploadData uploadData = new UploadData();
        uploadData.setFileId(secFile.getFileId());
        uploadData.setUploadPath(filePath);
        return uploadData;
    }

    @Override
    public String downloadFile(Long fileId) {
        SecFile file = getById(fileId);
        return file.getFilePath();
    }
}
